﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfApplication2.SindicatoWebService;
using WpfApplication2.UserControls;
using System.Windows.Controls;
using WpfApplication2.Helpers;
using System.Windows.Input;
using WpfApplication2.Views.Afiliado;
using WpfApplication2.Views;

namespace WpfApplication2.ViewModels.Afiliado
{
    public class GestionOrdenesViewModel : ViewModel
    {
        #region Properties

        public Service1SoapClient WS;
        public Instancias Instancias { get; set; }
        public AfiliadoDTO Afiliado { get; set; }

        public TipoOrdenDTO[] TiposOrdenCollection
        {
            get
            {
                return tiposOrden;
            }
            set
            {
                if (tiposOrden != value)
                {
                    tiposOrden = value;
                    RaisePropertyChanged("TiposOrdenCollection");
                }
            }
        }
        private TipoOrdenDTO[] tiposOrden;

        public List<PropertyElement> PropertyElementsCollection
        {
            get
            {
                return propertyElementsCollection;
            }
            set
            {
                if (propertyElementsCollection != value)
                {
                    propertyElementsCollection = value;
                    RaisePropertyChanged("PropertyElementsCollection");
                }
            }
        }
        private List<PropertyElement> propertyElementsCollection;

        #endregion


        public GestionOrdenesViewModel(Instancias instancias, AfiliadoDTO afiliado)
        {
            WS = new Service1SoapClient();
            Instancias = instancias;
            Afiliado = afiliado;
            TiposOrdenCollection = WS.GetOrdenesByPlanId(afiliado.TipoPlanId);
            PropertyElementsCollection = new List<PropertyElement>();
            
            var i = 1;
            foreach (var orden in TiposOrdenCollection)
            {
                PropertyElementsCollection.Add(
                    new PropertyElement() 
                    { 
                        Foreground = "White", 
                        Name = "Orden" + i.ToString(), 
                        Validations = { ValidationType.MayorIgualCero } 
                    });
                i++;
            }

            foreach (var prop in PropertyElementsCollection)
            {
                ValidationList.Add(prop);
            }
        }



        #region Command

        private DelegateCommand comprarOrdenesCommand;

        public ICommand ComprarOrdenesCommand
        {
            get
            {
                if (comprarOrdenesCommand == null)
                {
                    comprarOrdenesCommand = new DelegateCommand(ComprarOrdenes, CanExecute);
                }
                return comprarOrdenesCommand;
            }
        }

        private bool CanExecute()
        {
            return true;
        }

        private void ComprarOrdenes()
        {

            if (DataIsValid)
            {
                var fecha = DateTime.Now.ToShortDateString();
                for (var i = 0; i < PropertyElementsCollection.Count; i++)
                {
                    var prop = PropertyElementsCollection.ElementAt(i);
                    if (!string.IsNullOrEmpty(prop.Value) && prop.Value != "0")
                    {
                        for (int j = 0; j < Convert.ToInt32(prop.Value); j++)
			            {
                            var orden = new OrdenDTO()
                            {
                                AfiliadoId = Afiliado.Id,
                                Fecha = fecha,
                                TipoOrdenId = TiposOrdenCollection[i].Id
                            };
                            WS.GuardarOrden(orden);
			            }
                    }
                }
                Instancias.ModalDialog.ShowHandlerDialog("Las órdenes se han registrado correctamente.");
                Instancias.FrameContent.Content = new Home(); 
                
            }
            else
            {
                Instancias.ModalDialog.ShowHandlerDialog("ERROR: Verifique que las cantidades sean correctas.");
            }
        }

        #endregion

    }
}
